package com.vcg.vdam;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @ClassName TestSemaphore
 * @Description Semaphore 信号量不是保证线程安全；只是保证并发线程不超过多少数
 * @Author yangxing02
 * @Date 2020/11/6 15:08
 * @Version 1.0
 */
public class TestSemaphore {


    //测试信号得简单使用++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//    // 排队总人数（请求总数）
//    public static int clientTotal = 10;
//
//    // 可同时受理业务的窗口数量（同时并发执行的线程数）
//    public static int threadTotal = 2;
//
//    //
//    public static void main(String[] args) throws Exception {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        final Semaphore semaphore = new Semaphore(threadTotal);
//        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
//        for (int i = 0; i < clientTotal; i++) {
//            final int count = i;
//            executorService.execute(() -> {
//                try {
//                    semaphore.acquire(1);
//                    resolve(count);
//                    semaphore.release(1);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
////                countDownLatch.countDown();
//            });
//        }
////        countDownLatch.await();
//        executorService.shutdown();
//
//    }
//    private static void resolve(int i) throws InterruptedException {
//        System.out.println(System.currentTimeMillis()+"服务号{}，受理业务中。。。"+i);
//        Thread.sleep(2000);
//
//    }


    //测试信号得简单使用++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




    //测试a线程可以释放b线程通过信号量获取到得锁++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//    public static void main(String[] args) throws Exception{
//        final Semaphore semaphore = new Semaphore(threadTotal);
//        //a线程获取到锁；看b线程能否释放
//        semaphore.acquire(1);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                semaphore.release();
//            }
//        }).start();
//        Thread.sleep(500L);
//        System.out.println(semaphore.availablePermits());
//    }

//测试a线程可以释放b线程通过信号量获取到得锁++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++






//使用信号量并不能保证线程安全；只是保证并发数++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//    // 请求总数
//    public static int clientTotal = 5000;
//
//
//    // 同时并发执行的线程数
//    public static int threadTotal = 200;
//
//
//    public static int count = 0;
//
//
//    public static void main(String[] args) throws Exception {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        final Semaphore semaphore = new Semaphore(threadTotal);
//        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
//        for (int i = 0; i < clientTotal ; i++) {
//            executorService.execute(() -> {
//                try {
//                    semaphore.acquire();
//                    add();
//                    semaphore.release();
//                } catch (Exception e) {
//                    System.out.println("exception"+ e);
//                }
//                countDownLatch.countDown();
//            });
//        }
//        countDownLatch.await();
//        executorService.shutdown();
//        System.out.println("count:{}"+ count);
//    }
//
//
//    private static void add() {
//        count++;
//    }

    //使用信号量并不能保证线程安全；只是保证并发数++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
